﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace maintain_test.Geoinfor.CG.Beckett
{
    public static class CDateFormat
    {
        public static string HtmlDateToDBDate(string htmldate)
        {
            string sDBDate = string.Empty;
            try
            {
                if (htmldate.Length == 9)
                {
                    //當日期格式為9碼時，才執行轉換程序
                    sDBDate += (Convert.ToInt32(htmldate.Substring(0, 3)) + 1911).ToString("D4");
                    sDBDate += htmldate.Substring(4, 2);
                    sDBDate += htmldate.Substring(7, 2);
                }
            }
            catch (Exception ex)
            {
                string strErr = "台灣年(年份3碼)轉換九福資料庫的日期格式(8碼)時發生錯誤(" + ex.Message + ")";
                throw;
            }
            return sDBDate;
        }

        public static string DBDateToHtmlDate(string dbdate)
        {
            string sDBDate = string.Empty;
            try
            {
                if (dbdate.Length == 8)
                {
                    //當日期格式為8碼時，才執行轉換程序
                    sDBDate += (Convert.ToInt32(dbdate.Substring(0, 4)) - 1911).ToString("D3");
                    sDBDate +="/";
                    sDBDate += dbdate.Substring(4, 2);
                    sDBDate +="/";
                    sDBDate += dbdate.Substring(6, 2);
                }
            }
            catch (Exception ex)
            {
                string strErr = "九福資料庫的日期格式(8碼)轉換台灣年(年份3碼)時發生錯誤(" + ex.Message + ")";
                throw;
            }
            return sDBDate;
        }

        internal static string NowToDBDate()
        {
            string sDBDate = string.Empty;
            try
            {
                sDBDate += DateTime.Now.Year.ToString("D4");
                sDBDate += DateTime.Now.Month.ToString("D2");
                sDBDate += DateTime.Now.Day.ToString("D2");
            }
            catch (Exception ex)
            {
                string strErr = "現在日期轉換九福資料庫的日期格式(8碼)時發生錯誤(" + ex.Message + ")";
                throw;
            }
            return sDBDate;
        }
    }
}
